#include "Network.h"

TCPClient::TCPClient(boost::asio::io_service& IO_Service, tcp::resolver::iterator EndPointIter)
: m_IOService(IO_Service), m_Socket(IO_Service)
{
    tcp::endpoint EndPoint = *EndPointIter;

    m_Socket.async_connect(EndPoint,
            boost::bind(&TCPClient::OnConnect, this, boost::asio::placeholders::error, ++EndPointIter));
}

void TCPClient::Close()
{
    m_IOService.post(
        boost::bind(&TCPClient::DoClose, this));
}
void TCPClient::OnConnect(const boost::system::error_code& ErrorCode,
    tcp::resolver::iterator EndPointIter)
{
    if (ErrorCode == 0)
    // Successful connected
    {
        m_Socket.async_receive(boost::asio::buffer(m_Buffer.data(), m_BufLen),
                boost::bind(&TCPClient::OnReceive, this, boost::asio::placeholders::error));


    } else if (EndPointIter != tcp::resolver::iterator())
    {
        m_Socket.close();
        tcp::endpoint EndPoint = *EndPointIter;

        m_Socket.async_connect(EndPoint,
                boost::bind(&TCPClient::OnConnect, this, boost::asio::placeholders::error, ++EndPointIter));
    }
}

void TCPClient::OnReceive(const boost::system::error_code& ErrorCode)
{
    if (ErrorCode == 0)
    {
        std::cout << m_Buffer.data() << std::endl;

        m_Socket.async_receive(boost::asio::buffer(m_Buffer.data(), m_BufLen),
                boost::bind(&TCPClient::OnReceive, this, boost::asio::placeholders::error));
    } else {
        DoClose();
    }
}

void TCPClient::DoClose()
{
    m_Socket.close();
}
